﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Configuration;

namespace PK2
{
    [Export(typeof(IProcrastinationKit))]
    internal class ProcrastinationKit: IProcrastinationKit
    {
        [ImportMany(typeof(IPKPlugin))]
        internal List<IPKPlugin> _pluggins { get; set; }

        internal void Initialize()
        {
            this._pluggins = new List<IPKPlugin>();

            //Loads components on runtime----------------------------------------
            AggregateCatalog cat = new AggregateCatalog();
            cat.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            cat.Catalogs.Add(new DirectoryCatalog(Path.Combine(path,"Plugins")));

            CompositionContainer contnr = new CompositionContainer(cat);
            contnr.ComposeParts(this);
            //--------------------------------------------------------------------

            foreach (IPKPlugin plugin in _pluggins)
            {
                var cfg = ConfigurationManager.OpenExeConfiguration(plugin.GetType().Assembly.ManifestModule.FullyQualifiedName);

                plugin.Initialize(cfg);
            }

        }

        public void SetTextInTheStatusBar(string text)
        {
            System.Diagnostics.Debug.Print(text);
        }

    }
}
